<?php
/**
 * Application level Controller
 *
 * This file is application-wide controller file. You can put all
 * application-wide controller-related methods here.
 *
 * PHP 5
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */

App::uses('Controller', 'Controller');

/**
 * Application Controller
 *
 * Add your application-wide methods in the class below, your controllers
 * will inherit them.
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers.html#the-app-controller
 */
class AppController extends Controller {
	
	public $showCustomerComments = true;
	
	public $helpers = array(
		'Form',
		'Html' => array(
			'config' => 'html5_tags'
		)
	);
	
	public $top_photo;
	public $breadcrumbs;
	
	public function beforeFilter() {
		if($this->request->prefix == "admin") {
			$this->layout = "admin";
		}
	}
	
	public function beforeRender() {
		// load partners
		if(!$this->Partner) {
			$this->loadModel('Partner');
		}
		$partners = Set::extract('{n}.Partner', $this->Partner->find('all'));
		
		// set Top Photo
		$top_photo = $this->top_photo ? $this->top_photo : "default.jpg";
		$title = @$this->viewVars['title_for_layout'];
		$breadcrumbs = $this->breadcrumbs ? $this->breadcrumbs : array(array('label'=>$title?$title:$this->name,'link'=>false));
		
		// load Customer Comments
		$customer_comments = array();
		if ($this->showCustomerComments) {
			if (! $this->CustomerComment) {
				$this->loadModel('CustomerComment');
			}
			$customer_comments = $this->CustomerComment->find('all');
		}
        
        // load settings
        $settings = array();
        if (! $this->Setting) {
            $this->loadModel('Setting');
        }
        $settings = $this->Setting->find('list', array('fields' => array('name', 'value')));
        
		
		$this->set(compact('partners', 'top_photo', 'breadcrumbs', 'customer_comments', 'settings'));
	}
	
	public function home() {
		$this->layout = "home";
		$title_for_layout = "Eurogel d.o.o.";
		
		// photo slide-show
		$this->loadModel("Slideshow");
		$slideshow = $this->Slideshow->find('all', array('order'=>'order'));
		
		// news teaser
		$this->loadModel('News');
		$news_teaser = $this->News->find('all', array('limit' => 3));
		
		$this->set(compact('title_for_layout', 'slideshow', 'news_teaser'));
		
		$this->render("/Pages/home");
	}
	
	public function setFlash($message, $type="error") {
		$this->Session->setFlash($message, null, array('type'=>$type));
	}
	
	public function captcha() {
		$this->autoRender = false;
		
		$img = new Imagick();
		$draw = new ImagickDraw();
		$pixel = new ImagickPixel('white');
		
		$img->newImage(60, 24, $pixel);
		
		$draw->setFillColor('grey');
		$draw->setFont('img/Gabriola.ttf');
		$draw->setFontSize(26);
		
		$text = "";
		$buffer = "qwertyuiopasdfghjklzxcvbnm0123456789";
		for($i=0; $i<5; $i++) {
			$text .= substr($buffer, rand(0, strlen($buffer)-1), 1);
		}
		$this->Session->write("captcha", $text);
		
		$img->annotateImage($draw, 2, 18, 0, $text);
		
		$img->setImageFormat('png');		
		header("Content-Type: image/png");
		header("Content-Disposition:inline; filename=captcha.png");
		echo $img;
			
	}
	
	public function breadcrumbsSet($data) {
		$this->breadcrumbs = $data;
	}
	
	public function breadcrumbsAdd($label, $link=false) {
		$this->breadcrumbs[] = array(
			'label' => $label,
			'link' => $link
		);
	}
	
}
